﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QunYing.Model;
using Maticsoft.Common;

namespace QunYing.Web.market
{
    public partial class orderdetail : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                Repeater rp = ((Repeater)Master.FindControl("NavRepeater"));
                rp.DataSource = NavData();
                rp.DataBind();
            }
        }
        /// <summary>
        /// 快捷导航数据
        /// </summary>
        private DataTable NavData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("url");
            dt.Rows.Add("我的订单", "myorder.aspx");
            dt.Rows.Add("订单详情", "");
            return dt;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            if (Request["id"] == null)
            {
                return;
            }

            Model.OrderInfo model = new BLL.OrderInfo().GetModel(Convert.ToInt32(Request["id"]));

            DataTable dt = new BLL.OrderDetail().GetList("订单ID=" + model.订单ID).Tables[0];
            GridView1.DataSource = dt;
            GridView1.DataBind();

            lblTotalPrice.Text = model.总价 == null ? "0.00" : Convert.ToDecimal(model.总价).ToString("f2");
            lblUseScore.Text = model.总积分 == null ? "0" : Convert.ToDecimal(model.总积分).ToString("f0");
            lblGetScore.Text = model.赠送总积分 == null ? "0" : Convert.ToDecimal(model.赠送总积分).ToString("f0");
            txtNo.Text = model.订单编号;
            txtUser.Text = model.收件人;
            txtAddress.Text = model.收件地址;
            txtTel.Text = model.联系电话;
            txtMsg.Text = model.用户留言;

            DataTable dt2 = new BLL.OrderChange().GetList("订单ID=" + model.订单ID+" order by 操作时间 DESC").Tables[0];
            GridView2.DataSource = dt2;
            GridView2.DataBind();

            if (dt2.Rows.Count>0)
            {
                if (dt2.Rows[0]["订单状态"].ToString()==Enums.Start)
                {
                    btnCanel.Visible = true;
                }
                else
                {
                    btnCanel.Visible = false;
                }
            }
        }

        protected void btnCanel_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["userInfo"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            Model.OrderChange model = new Model.OrderChange();
            model.订单ID = Convert.ToInt32(Request["id"]);
            model.操作人 = ((ArrayList) Session["userInfo"])[1].ToString();
            model.操作时间 = DateTime.Now;
            model.订单状态 = Enums.Canel;
            int i=new BLL.OrderChange().Add(model);
            if (i>0)
            {
                QunYing.Model.Score score = new QunYing.BLL.Score().GetModel(((ArrayList)Session["userInfo"])[0].ToString());
                score.积分 = score.积分 + Convert.ToInt32(lblUseScore.Text) - Convert.ToInt32(lblGetScore.Text);
                QunYing.BLL.Score scoreBLL = new QunYing.BLL.Score();
                scoreBLL.Update(score);
                BindData();
            }
            else
            {
               MessageBox.Show(this,"取消失败!");
            }
        }


    }
}

